package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductServcie;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class ProductServcieImpl implements ProductServcie {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Override
    public PageInfo<Product> getProductByPage(Integer current, Integer size, ProductDto productDto) {
        PageHelper.startPage(current,size);
        List<Product> productList=productMapper.selectProductAll(productDto);
        return new PageInfo<>(productList);
    }

    /**
     * 添加商品信息
     * @param product
     */
    @Override
    public void save(Product product) {
        //添加Prodduct表中信息
//        product.setAuditStatus(0);
//        product.setStatus(0);
        productMapper.addProduct(product);

        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            //获取ProductSku对象
            ProductSku productSku = productSkuList.get(i);
            //设置商品编号
            productSku.setSkuCode(product.getId() + "_" + i);
            //设置skuName
            productSku.setSkuName(product.getName() +"-"+ productSku.getSkuSpec());
            //设置商品id
            productSku.setProductId(product.getId());
            productSku.setSaleNum(0);//设置销量
            productSku.setStatus(0);
            productSkuMapper.addSku(productSku);
        }
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.addDetails(productDetails);
        }

    @Override
    public Product getById(Long id) {
        //获取商品信息
        Product product=productMapper.selectById(id);

        //获取sku规格信息
        List<ProductSku> productSkuList=productSkuMapper.selectById(id);
        //设置skulist
        product.setProductSkuList(productSkuList);

        //设置图片地址
        ProductDetails productDetails=productDetailsMapper.selectById(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    public void updateById(Product product) {
        //根据id修改商品信息
        productMapper.updateById(product);
        //获取sku集合
        List<ProductSku> productSkuList = product.getProductSkuList();
        //修改sku商品数据
        productSkuList.forEach(productSku -> {
            productSkuMapper.updateById(productSku);
        });
        //设置图片地址
        ProductDetails productDetails = productDetailsMapper.selectById(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    @Override
    public void deleteById(Long id) {
        //根据id删除商品信息
        productMapper.deleteById(id);
        //根据商品id删除sku信息
        productSkuMapper.deleteById(id);
        //根据商品id删除图片信息
        productDetailsMapper.deleteById(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        //创建一个对象
        Product product = new Product();
        //设置主键
        product.setId(id);
        //设置审批状态值
        product.setAuditStatus(auditStatus);
        //判断审批状态值
        if (auditStatus.intValue()==1) {
            product.setAuditMessage("审批通过");
        }else {
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status==1) {
            product.setStatus(1);
        }else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }
}
